<?php

namespace Home\Controller;

use Think\Controller;

class RegController extends Controller {

    public function index(){
        $this->tjmail = I('uname');
        $this->display('reg');

    }

    

    /**
     * 发送邮箱
     */
    public function sendEmail()
    {
        $type = I('type')?I('type'):1;
        $where['email'] = I('email') ? I('email') : null;
        if (empty($where['email'])) {
            $data['status'] = - 1;
            $data['info'] = "请填写邮箱";
            $this->ajaxReturn($data);
        }
        if(!regex($where['email'],'email')){
           $data['status'] = - 1;
            $data['info'] = "请填写正确的邮箱";
            $this->ajaxReturn($data);
        }
        $code = rand(1000, 9999);
        //$content = "亲爱的用户,非常感谢您对星河墓的支持,您的邮箱验证码为{$code}，请妥善保管，不要告诉他人";
        $content = "亲爱的用户,您的邮箱验证码为{$code}";
        $r = setPostEmail($this->getConfig('EMAIL_HOST'), $this->getConfig('EMAIL_USERNAME'), $this->getConfig('EMAIL_PASSWORD'), $this->getConfig('name'), $where['email'], $config['name'] . '[注册]', $content);
        if ($r) {
            $data['status'] = - 3;
            $data['info'] = "服务器繁忙,请稍后重试";
            $this->ajaxReturn($data);
            exit();
        } else {
            $arr['code']=$code;
			$arr['email']=$where['email'];
			$arr['type']=$type;
			$arr['add_time']=time();
			M('email_code')->add($arr);
            $data['status'] = 1;
            $data['info'] = "邮箱发送成功";
            $this->ajaxReturn($data);
            exit();
        }
    }
    public function getConfig($value){
        $list=M("Config")->where(['name'=>$value])->find()['values'];
        return $list;
    }
    public function sendEmailTime($type){
        switch ($type){
            //注册
            case 1: $str['code']="zhuce_code";break;
            //找回密码
            case 2: $str['code']="zhaohui_code";break;
            default:$str['code']=-1;$str['time']="未知错误";break;
        }
        return $str;
    }
    public function logout(){
    	session_unset();

    	session_destroy();

    	$this->redirect('Login/index');

    }

    //驗證碼模塊
    function check_verify($code){

    	$verify = new \Think\Verify();

    	return $verify->check($code);

    }
    function verify() {

    	$config =    array(

    			'fontSize'    =>    16,    // 驗證碼字體大小

    			'length'      =>    5,     // 驗證碼位數

    			'useCurve'    =>    false, // 關閉驗證碼雜點

    		'useCurve' => false,

    	);

    	

    	$Verify = new \Think\Verify($config);

    	$Verify->codeSet = '0123456789';

    	$Verify->entry();

    }
    public function regadd() {
        $data_P = I ( 'post.' );
        header("Content-Type:text/html; charset=utf-8");
            if(!$this->check_verify($data_P['captcha_code'])){
                $return['status']=-5;
                $return['info']='验证码不正确';
               // $this->ajaxReturn($return);
            }
            $data_arr ["UE_account"] = $data_P ['UE_account'];
            $data_arr ["UE_verMail"] = $data_P ['UE_verMail'];
            $data_arr ["UE_accName"] = $data_P ['txtinvite'];
            $data_arr ["UE_password"] = MD5($data_P ['UE_password']);
            $data_arr ["UE_status"] = '0'; // 用户状态
            $data_arr ["UE_level"] = '0'; // 用户等级
            $data_arr ["UE_check"] = '1'; // 是否通过验证
            $data_arr ["UE_phone"] = $data_P ['UE_phone'];
            $data_arr ["UE_regIP"] = get_client_ip();
            $data_arr ["zcr"] = $data_P ['UE_account'];
            $data_arr ["UE_regTime"] = date ( 'Y-m-d H:i:s', time () );
            $data_arr ["UE_theme"] = $data_P ['UE_account'];
            $pwd = $data_P ['UE_password'];
            $repwd  = $data_P ['repwd'];
            if($pwd!=$repwd){
                $return['status']=-1;
                $return['info']='两次密码输入不一致';
                $this->ajaxReturn($return);
            }
            $user = M('User')->where(['UE_account'=>$data_P ['UE_account']])->find();
            if($user){
                $return['status']=-2;
                $return['info']='该用户名已存在';
                $this->ajaxReturn($return);
            }
            $email = M('User')->where(['UE_verMail'=>$data_P ['UE_verMail']])->find();
            if($user){
                $return['status']=-2;
                $return['info']='该邮箱已存在';
                $this->ajaxReturn($return);
            }
            $tuijian = $data_P ['txtinvite'];
            $tuijian_user = M('User')->where(['UE_account'=>$tuijian])->find();
            if(!$tuijian_user){
                $return['status']=-2;
                $return['info']='请填写正确的推荐人';
                $this->ajaxReturn($return);
            }
            $check_code=M('email_code')->where(["email"=>$data_P['UE_verMail'],'type'=>1])->order('add_time desc')->find();
            if($data_P['email_code']!= $check_code['code']||!$check_code){
                $data_return['status'] = -13;
                $data_return['info']   = '邮箱验证码有误';
                $this->ajaxReturn($data_return);
            }
           //删除验证码
		    M('email_code')->where(["email"=>$data_P['UE_verMail'],'type'=>1])->delete();
            $data = M('User')->add($data_arr);
            $return['status']=1;
            $return['info']='注册成功';
            $this->ajaxReturn($return);
    }
    public function forgotcl() {
        header("Content-Type:text/html; charset=utf-8");
        if (IS_POST) {
            $data_P = I ( 'post.' );
            $email = M('User')->where(['UE_verMail'=>$data_P ['email']])->find();
            if(!$email){
                $return['status']=-2;
                $return['info']='该用户不存在';
                $this->ajaxReturn($return);
            }
            if($data_P['password']!=$data_P['repassword']){
                $return['status']=-2;
                $return['info']='两次密码不一致';
                $this->ajaxReturn($return);
            }
            if($data_P ["email"]!=$_SESSION['email']||$data_P['email_code']!=$_SESSION['zhaohui_code'.$_SESSION['email']]){
                $return['status']=-5;
                $return['info']='邮箱验证码不正确';
                $this->ajaxReturn($return);
            }
            unset($_SESSION['zhaohui_code'.$_SESSION['email']]);
            unset($_SESSION['email']);
                $reg = M ( 'User' )->where (['UE_verMail'=>$data_P ['email']])->setField('UE_password', md5($data_P['password']));
                if ($reg) {
                    $return['status']=1;
                    $return['info']='找回成功';
                    $this->ajaxReturn($return);
                } else {
                    $return['status']=-1;
                    $return['info']='系统繁忙';
                    $this->ajaxReturn($return);
                }
        }
    }
}